package com.t.bricks.bussiness.db.dao.login.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.t.bricks.bussiness.db.dao.login.LoginDao;
import com.t.bricks.bussiness.model.login.LoginUser;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.data.IntegerUtil;
import com.t.bricks.utils.function.file.FileUtil;

@Component
public class LoginDaoImpl implements LoginDao {
	
	/**
	 * 按sId查询用户信息
	 * @param sId
	 * @return
	 */
	@Override
	public MsgEmity findById(String sId) {
		initDb();//初始化数据
		
		String filePath = String.format("%s/data/db/LoginUser.info", System.getProperty("user.dir"));

		MsgEmity me = FileUtil.readFile(filePath);
		if (!me.isSuccess()) {
			return me;
		}
		
		String txt = me.getData();
		if (null == txt || "".equals(txt)) {
			return MsgEmity.err(7001, "没有获取到数据");
		}
		
		LoginUser loginUser = null;
		String[] lines = txt.trim().split(System.lineSeparator());//分行
		for (String line : lines) {
			if (null == line || "".equals(line.trim())) {
				continue;
			}
			
			String[] array = line.trim().split("\t");
			if (array.length != 5) {
				Log.error("文件格式发现错误,行内分隔符错误");
				return MsgEmity.err(7002, "文件格式发现错误,行内分隔符错误");
			}
			
			if (sId.equals(array[0])) {
				continue;
			}

			loginUser = new LoginUser();
			loginUser.setsId(array[0]);
			loginUser.setsName(array[1]);
			loginUser.setsNo(array[2]);
			loginUser.setsPass(array[3]);
			loginUser.setiState(IntegerUtil.strToInt(array[4], 1));

			break;
		}
		
		if (null == loginUser) {
			return MsgEmity.err(7004, "未发现指定编号的信息");
		}
		
		return MsgEmity.success(loginUser, "查找成功");
	}
	
	/**
	 * 取用户名或工号对应的用户集合
	 * @param sNameOrNo
	 * @return
	 */
	@Override
	public MsgEmity findByNameOrNo(String sNameOrNo) {
		initDb();//初始化数据
		
		String filePath = String.format("%s/data/db/LoginUser.info", System.getProperty("user.dir"));
		
		MsgEmity me = FileUtil.readFile(filePath);
		if (!me.isSuccess()) {
			return me;
		}
		
		String txt = me.getData();
		if (null == txt || "".equals(txt)) {
			return MsgEmity.err(7001, "没有获取到数据");
		}

		List<LoginUser> list = new ArrayList<LoginUser>();
		String[] lines = txt.trim().split(System.lineSeparator());//分行
		for (String line : lines) {
			if (null == line || "".equals(line.trim())) {
				continue;
			}
			
			String[] array = line.trim().split("\t");
			if (array.length != 5) {
				Log.error("文件格式发现错误,行内分隔符错误");
				return MsgEmity.err(7002, "文件格式发现错误,行内分隔符错误");
			}
			
			if (!sNameOrNo.equals(array[1]) && !sNameOrNo.equals(array[2])) {
				continue;
			}
	
			LoginUser loginUser = new LoginUser();
			loginUser.setsId(array[0]);
			loginUser.setsName(array[1]);
			loginUser.setsNo(array[2]);
			loginUser.setsPass(array[3]);
			loginUser.setiState(IntegerUtil.strToInt(array[4], 1));
	
			list.add(loginUser);
		}
		
		if (list.size() < 1) {
			return MsgEmity.err(7004, "未发现指定编号的信息");
		}
		
		return MsgEmity.success(list, "查找成功");
	}
	
	/**
	 * 初始化数据
	 * @return
	 */
	private synchronized MsgEmity initDb() {
		String filePath = String.format("%s/data/db/LoginUser.info", System.getProperty("user.dir"));

		File file = new File(filePath);
		if (file.exists()) {
			return MsgEmity.success("文件已经存在");
		}
		
		if (FileUtil.saveStr(filePath, "00000001\tadmin\tadmin\t123456\t1") ) {
			return MsgEmity.success(7001, "保存失败!");
		}
		
		return MsgEmity.success(7999, "创建成功!");
	}
	
}
